import { tasksEnd, tasksStart } from "$api/tasks"
import { BASIC_FLAG } from "$utils/enum"
const { START, END } = BASIC_FLAG
// 对话框
export default {
  data() {
    return {
      dialogParams: {
        // 是否可见
        visible: false,
        content: "确定{{titleStatus}}{{taskName}}任务数据?",
        // 需要调用的方法
        apis: [
          {
            flag: START,
            api: tasksStart
          },
          {
            flag: END,
            api: tasksEnd
          },
        ],
        // 当前弹框使用的标识
        useFlag: -1,
        // 接口参数
        params: {
          id: null,
        },
        // 当前选中数据
        selectRowData: {}
      },
      mp3_success: [
        "/static/mp3/success.mp3",
        "/static/mp3/success2.mp3",
        "/static/mp3/success3.mp3",
        "/static/mp3/success4.mp3"
      ],
      mp3_start: [
        "/static/mp3/start1.mp3",
        "/static/mp3/start2.mp3",
        "/static/mp3/start3.mp3",
      ]
    }
  },
  methods: {

    scanAudio() {
      let music = null;
      music = uni.createInnerAudioContext(); // 创建播放器对象
      music.src = "";
      if (this.dialogParams.useFlag === START) {
        music.src = this.mp3_start[this.generateRandomInteger(0, 2)]
      } else {
        music.src = this.mp3_success[this.generateRandomInteger(0, 3)]
      }

      music.play(); // 执行执行播放
      music.onEnded(() => {
        // 音频播放结束
        music = null;
      });
    },
    // 打开弹框
    openDialog(item, flag) {
      Object.assign(this.dialogParams, {
        visible: true,
        selectRowData: item,
        useFlag: flag
      })
    },
    generateRandomInteger(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    },
    modalSubmit() {
      this.search()
      this.scanAudio()
    }
  }
}
